<?php
namespace Site\Controller;
use Common\Controller\AdminbaseController;
class UserController extends AdminbaseController 
{   
	protected $userObj;
    protected $roleObj;
    public function _initialize()
    {
          parent::_initialize();
          $this->userObj = D("User");
          $this->roleObj = M("User_role");
    }

	public function index()
	{
		
		$this->lists();
	}

	public function lists()
	{
	    $count      = $this->userObj->count();
        $Page       = new \Think\Page($count,15);
        $show       = $Page->show();
        $list =  $this->userObj->relation(true)->order(array("id" => "ASC"))->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show); 
        $this->display('list');
	}
    public function getRoles()
    {
        $list =$this->roleObj->order(array("id" => "ASC"))->select();
        return $list;
    }
    public function add()
    {
        $roleObjs = $this->getRoles();
        $this->assign("roles",$roleObjs);
    	$this->display("add");
    }
    public function edit($id=0)
    {
    	if ($id>0) 
    	{
            $roleObjs = $this->getRoles();
            $this->assign("roles",$roleObjs);
	    	$userObj = $this->userObj->find($id);
            $this->assign("user",$userObj);
            $this->display("edit");
    	}else{
            header("404");
        }
    }
    public function save()
    {
        $id = I('post.inputId','-1','htmlspecialchars');
        $roleid = I('post.inputRole','','htmlspecialchars');
        $nickname = I('post.inputName','','htmlspecialchars');
        $email = I('post.inputEmail','','htmlspecialchars');
        $password = I('post.inputPassword','','htmlspecialchars');
        $status = I('post.inputStatus','0','htmlspecialchars');
        $data = array();
        $data["roleid"]  = $roleid;
        $data["nickname"]  = $nickname;
        $data["email"]  = $email;
        if(!empty($password)) $data["password"] = md5("lock".$password);
        $data["status"] = $status;
        if($id>0)
        {
            $editResult = $this->userObj->where("id=%d",$id)->save($data);
            $arr = array("status"=>'1',"message"=>"编辑管理员成功","url"=>"index");
        }
        else
        {
           $data['createtime'] =time();

            $addResult =  $this->userObj->add($data);
            if($addResult) {
                $arr = array("status"=>'1',"message"=>"添加管理员成功","url"=>"index");
            }else{
                $arr = array("status"=>'0',"message"=>"添加管理员失败");
            }
        }
        echo json_encode($arr);
    }
   /*单个删除 id*/
    public function delete($id)
    {
        parent::_delete($this->userObj,$id);
    }

    /* 批量删除 ids*/
    public function checkDeletes($ids)
    {
      parent::_checkDeletes($this->userObj,$ids);
    }
    /* 单个修改当前状态status 0 1 -1*/
    public function changeStatus($id,$status)
    {
        parent::_changeStatus($this->userObj,$id,$status);
    }
    /*批量 修改当前状态status 0 1 -1*/
    public function checkChanges($status,$ids)
    {
      parent::_checkChanges($this->userObj,$status,$ids);
    }
    // 检查用户名是否已存在
    public function validNickname(){
      $map['nickname'] =  $_POST["param"];
      $result = $this->userObj->where($map)->select();
      if(empty($result)){
        echo "y";
      }else{
        echo "<i class='icon-warning-sign'></i> 此用户名已存在";
      }
    }
     // 检查用户名是否已存在
    public function validEmail(){
      $map['email'] =  $_POST["param"];
      $result = $this->userObj->where($map)->select();
      if(empty($result)){
        echo "y";
      }else{
        echo "<i class='icon-warning-sign'></i> 此邮箱已存在";
      }
    }
    
}